package double_pointer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-11-30
 * Time: 16:00
 */
public class DuplicateZeros1089 {
    public static void duplicateZeros(int[] arr) {
        int cur = 0, dest = -1;
        while (cur < arr.length) {
            if(arr[cur] == 0) {
                dest += 2;
            } else {
                dest++;
            }
            if(dest >= arr.length - 1) {
                break;
            }
            cur++;
        }
        if(dest >= arr.length) {
            arr[arr.length - 1] = 0;
            dest -= 2;
            cur--;
        }
        while (cur >= 0) {
            if(arr[cur] == 0) {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            } else {
                arr[dest--] = arr[cur--];
            }
        }
    }

    public static void main(String[] args) {
        duplicateZeros(new int[]{1,0,2,3,0,4,5,0});
    }
}
